var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import React, { useMemo, useRef, useState } from "react";
import { FloatLayout as FloatLayoutCustom } from "../components/index";
export var TopContext = React.createContext({});
var TopProvider = function (props) {
    var _a = __read(useState(false), 2), opened = _a[0], setOpened = _a[1];
    var _b = __read(useState(), 2), customeView = _b[0], setCustomeView = _b[1];
    var optionsRef = useRef();
    var onCloseRef = useRef();
    var currentComponentId = useRef(); // 当前显示的组件id
    var popup = function (compObj, options) {
        if (options === void 0) { options = {}; }
        optionsRef.current = __assign(__assign({}, options), { closeable: (options.closeable === undefined ? true : options.closeable) });
        currentComponentId.current = compObj.id;
        setCustomeView(compObj.component);
        setOpened(true);
        return new Promise(function (resolve) {
            onCloseRef.current = resolve;
        });
    };
    var topView = useMemo(function () {
        var _a, _b, _c, _d, _e;
        return React.createElement(FloatLayoutCustom, { isOpened: opened, onClose: function () {
                var _a;
                if (optionsRef.current && optionsRef.current.closeable) {
                    // 清除当前组件id
                    currentComponentId.current = 0;
                    setOpened(false);
                    optionsRef.current && optionsRef.current.hasInput && setCustomeView(null);
                    (_a = onCloseRef.current) === null || _a === void 0 ? void 0 : _a.call(onCloseRef, true);
                }
            }, isCover: (_a = optionsRef.current) === null || _a === void 0 ? void 0 : _a.isCover, style: optionsRef.current && optionsRef.current.type === 'slid-up' ? "sheet" : 'alert', floatStyle: (_b = optionsRef.current) === null || _b === void 0 ? void 0 : _b.floatStyle, maskStyle: (_c = optionsRef.current) === null || _c === void 0 ? void 0 : _c.maskStyle, floatClassName: (_d = optionsRef.current) === null || _d === void 0 ? void 0 : _d.floatClassName, containerStyle: (_e = optionsRef.current) === null || _e === void 0 ? void 0 : _e.containerStyle }, customeView);
    }, [opened, customeView]);
    var close = function (componentId) {
        // 与当前显示的为同一组件
        if (currentComponentId.current == componentId) {
            // 清除当前组件id
            currentComponentId.current = 0;
            forcedClose();
        }
    };
    var forcedClose = function () {
        optionsRef.current && optionsRef.current.hasInput && setCustomeView(null);
        setOpened(false);
    };
    return (React.createElement(TopContext.Provider, { value: { popup: popup, close: close, forcedClose: forcedClose, topView: topView } }, props.children));
};
export default TopProvider;
